from flask import Blueprint, render_template, request, session, url_for, redirect
from .models import Category, Post, Friend_post, Events_post

from sqlalchemy import or_

from ..auth.views.auth import login_required

bp = Blueprint('blog', __name__, url_prefix='/blog', static_folder='static', template_folder='templates')


@bp.route('/')
def index():
    return render_template('index.html')


@bp.route('/category/Events')
@login_required
def cates_Events():
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    page = request.args.get('page', 1, type=int)
    user_id = session['user_id']
    pagination = Events_post.query.filter(Events_post.category_id == cate_id, Events_post.user_id == user_id).paginate(
        page=page,
        per_page=10,
        error_out=False)
    post_list = pagination.items
    return render_template('Events.html', cate=cate, post_list=post_list, cate_id=cate_id,
                           pagination=pagination)


@bp.route('/category/Events/<int:post_id>', methods=['GET', 'POST'])
def event_detail(post_id):
    # 详情页
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    post = Events_post.query.get_or_404(post_id)
    return render_template('detail.html', cate=cate, post=post)


@bp.route('/category/Friend')
@login_required
def cates_Friend():
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    page = request.args.get('page', 1, type=int)
    user_id = session['user_id']
    pagination1 = Friend_post.query.filter(Friend_post.user_id == user_id,
                                           Friend_post.new_old == 1).all()
    post_list1 = pagination1

    pagination2 = Friend_post.query.filter(Friend_post.user_id == user_id,
                                           Friend_post.new_old == 2).all()
    post_list2 = pagination2
    return render_template('Friend.html', cate=cate, post_list1=post_list1, post_list2=post_list2, cate_id=cate_id)


@bp.route('/category/Friend/<int:post_id>', methods=['GET', 'POST'])
def friend_detail(post_id):
    # 详情页
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    post = Friend_post.query.get_or_404(post_id)
    return render_template('detail.html', cate=cate, post=post)


@bp.route('/category/Family_ancestry')
@login_required
def cates_Family_ancestry():
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    page = request.args.get('page', 1, type=int)
    user_id = session['user_id']
    pagination = Post.query.filter(Post.category_id == cate_id, Post.user_id == user_id).paginate(page=page,
                                                                                                  per_page=10,
                                                                                                  error_out=False)
    post_list = pagination.items
    family_members = [
        {
            'name': '张三', 'relation': '祖父', 'image': '/blog/static/img/gp.png',
            'children': [
                {
                    'name': '李四', 'relation': '父亲', 'image': '/blog/static/img/bb.png',
                    'children': [
                        {'name': '王五', 'relation': '儿子', 'image': '/blog/static/img/ez.png'},
                        {'name': '赵六', 'relation': '女儿', 'image': '/blog/static/img/ne.png'}
                    ]
                },
                {
                    'name': '孙七', 'relation': '叔叔', 'image': '/blog/static/img/ss.png',
                    'children': [
                        {'name': '周八', 'relation': '侄子', 'image': '/blog/static/img/zz.png'},
                        {'name': '吴九', 'relation': '侄女', 'image': '/blog/static/img/zn.png'}
                    ]
                }
            ]
        }
    ]

    return render_template('Family_ancestry.html', cate=cate, post_list=post_list, cate_id=cate_id,
                           pagination=pagination, family_members=family_members)


@bp.route('/category/Family_ancestry1', methods=['POST'])
@login_required
def Family_ancestry1():
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    page = request.args.get('page', 1, type=int)
    user_id = session['user_id']
    pagination = Post.query.filter(Post.category_id == cate_id, Post.user_id == user_id).paginate(page=page,
                                                                                                  per_page=10,
                                                                                                  error_out=False)
    post_list = pagination.items
    family_members = [
        {
            'name': '张三', 'relation': '祖父', 'image': '/blog/static/img/gp.png',
            'children': [
                {
                    'name': '李四', 'relation': '父亲', 'image': '/blog/static/img/bb.png',
                    'children': [
                        {'name': '王五', 'relation': '儿子', 'image': '/blog/static/img/ez.png'},
                        {'name': '赵六', 'relation': '女儿', 'image': '/blog/static/img/ne.png'}
                    ]
                },
                {
                    'name': '孙七', 'relation': '叔叔', 'image': '/blog/static/img/ss.png',
                    'children': [
                        {'name': '周八', 'relation': '侄子', 'image': '/blog/static/img/zz.png'},
                        {'name': '吴九', 'relation': '侄女', 'image': '/blog/static/img/zn.png'}
                    ]
                }
            ]
        },
        # 可以添加其他家族成员，例如张三的配偶、兄弟姐妹等
        {
            'name': '钱十', 'relation': '祖母', 'image': '/blog/static/img/gm.png',
            # 祖母的子女及孙辈成员...
        }
        # 其他家族成员...
    ]

    return render_template('Family_ancestry.html', cate=cate, post_list=post_list, cate_id=cate_id,
                           pagination=pagination, family_members=family_members)


@bp.route('/category/Buck_list')
@login_required
def cates_Buck_list():
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    page = request.args.get('page', 1, type=int)
    user_id = session['user_id']
    pagination = Post.query.filter(Post.category_id == cate_id, Post.user_id == user_id).paginate(page=page,
                                                                                                  per_page=10,
                                                                                                  error_out=False)
    post_list = pagination.items
    return render_template('Buck_list.html', cate=cate, post_list=post_list, cate_id=cate_id,
                           pagination=pagination)


@bp.route('/category/Just_happen')
@login_required
def cates_Just_happen():
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    page = request.args.get('page', 1, type=int)
    user_id = session['user_id']
    pagination = Post.query.filter(Post.category_id == cate_id, Post.user_id == user_id).paginate(page=page,
                                                                                                  per_page=10,
                                                                                                  error_out=False)
    post_list = pagination.items
    return render_template('Just_happen.html', cate=cate, post_list=post_list, cate_id=cate_id,
                           pagination=pagination)


@bp.route('/category/Travel')
@login_required
def cates_Travel():
    cate_id = request.args.get('cate_id')
    cate = Category.query.get(cate_id)
    page = request.args.get('page', 1, type=int)
    user_id = session['user_id']
    pagination = Post.query.filter(Post.category_id == cate_id, Post.user_id == user_id).paginate(page=page,
                                                                                                  per_page=10,
                                                                                                  error_out=False)
    post_list = pagination.items
    return render_template('Travel.html', cate=cate, post_list=post_list, cate_id=cate_id,
                           pagination=pagination)


@bp.route('/category/<int:cate_id>')
@login_required
def cates(cate_id):
    # 分类页
    cate = Category.query.get(cate_id)
    if cate.name == 'Events':
        return redirect(url_for('blog.cates_Events', cate_id=cate_id))
    if cate.name == 'Family_ancestry':
        return redirect(url_for('blog.cates_Family_ancestry', cate_id=cate_id))
    if cate.name == 'Buck_list':
        return redirect(url_for('blog.cates_Buck_list', cate_id=cate_id))
    if cate.name == 'Travel':
        return redirect(url_for('blog.cates_Travel', cate_id=cate_id))
    if cate.name == 'Friend_history':
        return redirect(url_for('blog.cates_Friend', cate_id=cate_id))
    if cate.name == 'Just_happen':
        return redirect(url_for('blog.cates_Just_happen', cate_id=cate_id))


@bp.route('/category/<int:cate_id>/<int:post_id>', methods=['GET', 'POST'])
def detail(cate_id, post_id):
    # 详情页
    if cate_id == 3:
        return redirect(url_for('blog.event_detail', cate_id=cate_id, post_id=post_id))
    elif cate_id == 2:
        return redirect(url_for('blog.friend_detail', cate_id=cate_id, post_id=post_id))

    # 其他几个搜索的还没写，如有需要，按章上面的添加。



@bp.route('/search')
def search():
    # 搜索视图
    words = request.args.get('words', '', type=str)
    page = request.args.get('page', 1, type=int)
    cate_id = request.args.get('cate_id')
    if cate_id == '3':  # 3类别为events,使用events表搜索
        pagination = Events_post.query.filter(
            or_(Events_post.title.like("%" + words + "%"), Events_post.desc.like("%" + words + "%"))).paginate(
            page=page, per_page=10,
            error_out=False)
    elif cate_id == '2':  # 2类是朋友，使用朋友表搜索
        pagination = Friend_post.query.filter(
            or_(Friend_post.title.like("%" + words + "%"), Friend_post.desc.like("%" + words + "%"))).paginate(
            page=page, per_page=10,
            error_out=False)
    else:
        pagination = Post.query.filter(or_(Post.title.like("%" + words + "%"), Post.desc.like("%" + words + "%"),
                                           Post.content.like("%" + words + "%"))).paginate(page=page, per_page=10,
                                                                                           error_out=False)

    # 其他几个搜索的还没写，如有需要，按章上面的添加。
    post_list = pagination.items
    return render_template('search.html', post_list=post_list, words=words, pagination=pagination)
